import { insertInfoDataItem, getMenoDataLike, getMeonDataAll, getMeonDataById, deleteMenoDataItemById, getMenoDataTop4 } from '../db/meon';
import request from '../util/requset';
import { isOffLine } from '../util/isOffLine'

/**
 * 新增、编辑
 * @param {*} param 
 * @returns 
 */
export async function editMenoItemApi(param) {
    const url = param.id !== undefined ? '/meno/updateMeno' : '/meno/insterMeno'
    const method = param.id !== undefined ? 'PUT' : 'POST';
    return  isOffLine() ? await insertInfoDataItem(param):await request(url,{method,data:param});
}

/**
 * 获取全部数据
 */
export async function getMenoDataAllApi() {
    return isOffLine() ? await getMeonDataAll():await request('/meno/queryMenoAll');
}

/**
 * 便签根据id获取对应数据
 * @param {*} id 
 * @returns 
 */
export async function getMenoDataByIdApi(id) {
    return isOffLine() ? await getMeonDataById(id):await request(`/meno/queryMenoById?id=${id}`);
}


/**
 * 便签根据id删除单条
 * @param {*} id 
 * @returns 
 */
export async function deleteMenoDataItemByIdApi(id) {
    return isOffLine() ? await deleteMenoDataItemById(id):await request(`/meno/deleteMenoById?id=${id}`,{method:'DELETE'});;
} 

/**
 * 模糊查询
 * @param {*} params 
 * @returns 
 */
export async function getMenoItemLikeApi(params) {
    return isOffLine() ? await getMenoDataLike(params):await request(`/meno/queryLikeMeno?name=${params.name}`)
}

/**
 * 获取前4条数据
 * @returns 
 */
export async function getMenoDataTop4Api() {
    return isOffLine() ? await getMenoDataTop4() : await request('/meno/queryMenoTop4');
}